import { unref, computed } from 'vue';
import {
  DatePicker,
  WeekPicker,
  MonthPicker,
  QuarterPicker,
  YearPicker
} from '@arco-design/web-vue';
import { TimeFrameType } from './type';

// 根据指标配置中的时间粒度获取时间组件
export const getCurrentTimeComponent = (timeUnit: TimeFrameType) => {
  return unref(
    computed(() => {
      switch (timeUnit) {
        case TimeFrameType.DAY:
          return DatePicker;
        case TimeFrameType.WEEK:
          return WeekPicker;
        case TimeFrameType.MONTH:
          return MonthPicker;
        case TimeFrameType.QUARTER:
          return QuarterPicker;
        case TimeFrameType.YEAR:
          return YearPicker;
        default:
          return DatePicker;
      }
    })
  );
};

// 统计周期时间粒度规则
export const getStatisticalPeriodOption = (
  minFrame: TimeFrameType,
  option: Array<{ label: string; value: string }>
) => {
  return unref(
    computed(() => {
      let res;
      switch (minFrame) {
        case TimeFrameType.DAY:
          res = option;
          break;
        case TimeFrameType.WEEK:
          res = option.filter((item) => item.value === TimeFrameType.WEEK);
          break;
        case TimeFrameType.MONTH:
          res = option.filter((item) =>
            (
              [
                TimeFrameType.MONTH,
                TimeFrameType.QUARTER,
                TimeFrameType.YEAR
              ] as string[]
            ).includes(item.value)
          );
          break;
        case TimeFrameType.QUARTER:
          res = option.filter((item) =>
            ([TimeFrameType.QUARTER, TimeFrameType.YEAR] as string[]).includes(
              item.value
            )
          );
          break;
        case TimeFrameType.YEAR:
          res = option.filter((item) => item.value === TimeFrameType.YEAR);
          break;
        default:
          res = option;
          break;
      }
      return res.some((item) => item.value === 'recent')
        ? res
        : res.concat(option.filter((item) => item.value === 'recent'));
    })
  );
};

const addZero = (t: string) => {
  return t.length > 1 ? t : '0' + t;
};

export const quarterFormater = (
  t: string,
  format: TimeFrameType.QUARTER | TimeFrameType.MONTH
) => {
  if (!t) {
    return t;
  }
  let res;
  if (t.includes('Q')) {
    if (format === TimeFrameType.QUARTER) {
      res = t;
    } else {
      res = t.slice(0, -2) + '-' + addZero(+t.slice(-1) * 3 - 2 + '');
    }
  } else {
    if (format === TimeFrameType.QUARTER) {
      res = t.slice(0, -3) + 'Q' + Math.ceil(+t.slice(-2) / 3);
    } else {
      res = t;
    }
  }
  return res;
};

// 处理季度时间格式
export const quarterTimeFormater = (
  model: any,
  format: TimeFrameType.QUARTER | TimeFrameType.MONTH
) => {
  if (
    model.metricsSetting.statisticalPeriod.timeUnit !== TimeFrameType.QUARTER
  ) {
    return;
  }
  model.compares.forEach((item: any) => {
    if (
      item.dateCondition.dateMode === 'FIXDATE' &&
      item.dateCondition.timeUnit === TimeFrameType.QUARTER
    ) {
      item.dateCondition.fixDate = quarterFormater(
        item.dateCondition.fixDate,
        format
      );
    }
  });
  if (model.progessTrack && model.progessTrack.startDate) {
    model.progessTrack.startDate = quarterFormater(
      model.progessTrack.startDate,
      format
    );
  }
  if (model.progessTrack && model.progessTrack.endDate) {
    model.progessTrack.endDate = quarterFormater(
      model.progessTrack.endDate,
      format
    );
  }
  if (model.dataTime) {
    model.dataTime = quarterFormater(model.dataTime, format);
  }
  if (model.dateCondition && model.dateCondition.dateMode === 'BEFORE') {
    model.dateCondition.fixDate = quarterFormater(
      model.dateCondition.fixDate,
      format
    );
  }
};
